カウンターのソースはこんな感じ。
class	webCounter	{
	private	$m_hMemory;
	private	$m_szFilename;
	private	$m_nLastError;
	private $m_nSzLastError;
	private $m_nExpire;
	private $m_nCounterLength;

	function	__construct($filename) {
		$this->m_hMemory = new memcached();
		$this->m_szFilename = $filename;
		$this->m_nLastError = 0;
		$this->m_nSzLastError = "";
		$this->m_nExpire = 24*60*60;
		$this->m_nCounterLength = 8;
	}

	function	__destruct() {
		unset($this->hMemory);
	}

	// -----------------------------------------------------------------
	// input	:	key
	// output	:	none
	// return	:	false when failed.
	//			othrwise return array.
	//				array(0) = web counter.
	//				array(1) = count to key access.
	// -----------------------------------------------------------------
	public	function	getCount($szUniqKey) {

		$ret = $this->m_hMemory->getServerList();
		if ( empty($ret) ) {
			$ret = $this->m_hMemory->addServer("localhost", 11211);
			if ( $ret !== true ) {
				$this->m_nLastError = $this->m_hMemory->getResultCode();
				$this->m_nSzLastError = $this->m_hMemory->getResultMessage();
				return(false);
			}
		}

		$value = $this->m_hMemory->get($szUniqKey);
		if ( $value !== false ) {
			$count = $this->currentCount();
			return($count);
		}

		$ret = $this->m_hMemory->set($szUniqKey, 1, $this->m_nExpire);
		if ( $ret !== true ) {
		}

		$count = $this->incleseWebCounter();
		return($count);
	}

	private	function	incleseWebCounter() {

		$fp = fopen($this->m_szFilename, 'r+');
		if ($fp) {
			if (flock($fp, LOCK_EX) === false) {
				return(0);
			}
		}

		$counter = (int) fgets($fp, $this->m_nCounterLength);
		$counter++;
		rewind($fp);
		if (fwrite($fp,  $counter) === FALSE) {
			flock ($fp, LOCK_UN);
			fclose ($fp);
			return(0);
		}
		flock ($fp, LOCK_UN);
		fclose ($fp);

		$fotmat = sprintf("%%0%dd", $this->m_nCounterLength);
		$counter = sprintf($fotmat, $counter);
		return($counter);
	}

	private	function	currentCount() {

		$fp = fopen($this->m_szFilename, 'r');
		if ($fp) {
			if (flock($fp, LOCK_SH) === false) {
				return(0);
			}
		}

		$counter = (int) fgets($fp, $this->m_nCounterLength);
		flock ($fp, LOCK_UN);
		fclose ($fp);

//		$fotmat = sprintf("%%0%dd", $this->m_nCounterLength);
//		$counter = sprintf($fotmat, $counter);
		return($counter);
	}
}
これをこんなカンジで呼び出します。
	$counter_lenght = 8;
	$webCounter = new webCounter($gblSzCounter);
        $counter = $webCounter->getCount($_SERVER["REMOTE_ADDR"]);
	$fotmat = sprintf("%%0%dd", $counter_lenght);
	$counter = sprintf($fotmat, $counter);
	echo  'Counter: ' . $counter ;
$gblSzCounterにはカウンターファイル名称が格納されています。
TOC
top - 22:03:19 up 45 min,  2 users,  load average: 0.00, 0.03, 0.00
Tasks: 171 total,   1 running, 113 sleeping,   1 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 98.9 id,  0.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   948296 total,   574888 free,   116216 used,   257192 buff/cache
KiB Swap:   102396 total,   102396 free,        0 used.   758844 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1590 root      20   0    7748   3332   2764 R  10.0  0.4   0:00.07 top
    1 root      20   0   28104   6136   4912 S   0.0  0.6   0:03.18 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
    3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_gp
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp
    5 root      20   0       0      0      0 I   0.0  0.0   0:00.03 kworker/0:0-eve
    8 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.15 ksoftirqd/0
   10 root      20   0       0      0      0 I   0.0  0.0   0:01.50 rcu_sched
   11 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_bh
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/0
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1
   15 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/1
   16 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/1
   19 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2
   20 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/2
   21 root      20   0       0      0      0 S   0.0  0.0   0:00.11 ksoftirqd/2
   24 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/3
   25 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/3
   26 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/3
   29 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs
   30 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 netns
   34 root      20   0       0      0      0 I   0.0  0.0   0:00.61 kworker/3:1-eve
   35 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd
   36 root      20   0       0      0      0 S   0.0  0.0   0:00.00 oom_reaper
   37 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 writeback
   38 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kcompactd0
   39 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 crypto
   40 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kblockd
   41 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 watchdogd
   42 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rpciod
   43 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/u9:0-hc
   44 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 xprtiod
   47 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kswapd0
   48 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 nfsiod
   59 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kthrotld
   60 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 iscsi_eh
   61 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 dwc_otg
   62 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 DWC Notificatio
   63 root       1 -19       0      0      0 S   0.0  0.0   0:00.00 vchiq-slot/0
   64 root       1 -19       0      0      0 S   0.0  0.0   0:00.00 vchiq-recy/0
   65 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 vchiq-sync/0
   66 root      20   0       0      0      0 S   0.0  0.0   0:00.00 vchiq-keep/0
   67 root      10 -10       0      0      0 S   0.0  0.0   0:00.00 SMIO
   69 root     -51   0       0      0      0 S   0.0  0.0   0:00.10 irq/86-mmc1
   71 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mmc_complete
   72 root       0 -20       0      0      0 I   0.0  0.0   0:00.15 kworker/2:1H-kb
   73 root       0 -20       0      0      0 I   0.0  0.0   0:00.39 kworker/0:1H-mm
   74 root       0 -20       0      0      0 I   0.0  0.0   0:00.20 kworker/1:1H-kb

ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/root         30G  8.5G   20G   31% /
devtmpfs         459M     0  459M    0% /dev
tmpfs            464M     0  464M    0% /dev/shm
tmpfs            464M   13M  451M    3% /run
tmpfs            5.0M  4.0K  5.0M    1% /run/lock
tmpfs            464M     0  464M    0% /sys/fs/cgroup
/dev/sda1         59G  5.7G   50G   11% /media/pi
/dev/mmcblk0p1    43M   23M   20M   54% /boot
tmpfs             93M     0   93M    0% /run/user/1000
tmpfs             93M     0   93M    0% /run/user/1001

平和ですなぁ・・・
TOP